<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright  Copyright (c) 2006, Julien PORTALIER
 * @link       http://portalier.com/feather
 * @license    http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class Resampler
{
	protected $filename;
#	protected $image;
	public    $size;
	
	function __construct($filename=null)
	{
		if (!is_null($filename))
			$this->load($filename);
	}
	
	function __destruct()
	{
		$this->destroy();
	}
	
	function __get($attr)
	{
		if ($attr == 'image')
		{
			switch ($this->size['mime'])
			{
				case 'image/jpeg': $this->image =& imagecreatefromjpeg($this->filename); break;
				case 'image/png':  $this->image =& imagecreatefrompng($this->filename);  break;
				case 'image/gif':  $this->image =& imagecreatefromgif($this->filename);  break;
				default: return false;
			}
			return $this->image;
		}
		return false;
	}
	
	function destroy()
	{
		if (!empty($this->image))
			imagedestroy($this->image);
	}
	
	function load($filename)
	{
		if (!file_exists($filename))
			return false;
		$this->size = getimagesize($filename);
		
		$this->filename = $filename;
		/*
		switch ($this->size['mime'])
		{
			case 'image/jpeg': $this->image =& imagecreatefromjpeg($filename); break;
			case 'image/png':  $this->image =& imagecreatefrompng($filename);  break;
			case 'image/gif':  $this->image =& imagecreatefromgif($filename);  break;
			default: return false;
		}
		*/
	}
	
	function savePNG($dest_file)
	{
		return imagepng($this->image, $dest_file, 9);
	}
	
	/**
	 * @todo Crop doesn't work as expected if $max_w != $max_h (non square)
	 */
	function create($dest_file, $max_w=100, $max_h=100, $crop=false)
	{
		$src_w = $this->size[0];
		$src_h = $this->size[1];
		$src_x = 0;
		$src_y = 0;
		
		if (!$crop)
		{
			// resamples with aspect ratio
			if ($src_w > $src_h)
			{
				$dest_w = $max_w;
				$dest_h = $src_h / $src_w * $dest_w;
				if ($dest_h > $max_h)
				{
					$dest_h = $max_h;
					$dest_w = $src_w / $src_h * $dest_h;
				}
			}
			elseif ($src_h > $src_w)
			{
				$dest_h = $max_h;
				$dest_w = $src_w / $src_h * $dest_h;
				if ($dest_w > $max_w)
				{
					$dest_w = $max_w;
					$dest_h = $src_h / $src_w * $dest_w;
				}
			}
			else
			{
				$dest_w = ($max_w > $max_h) ? $max_h : $max_w;
				$dest_h = $dest_w;
			}
		}
		else
		{
			// resamples by cropping original image to given ratio
			$dest_w = $max_w;
			$dest_h = $max_h;
			
			if ($src_w > $src_h)
			{
				$src_y = 0;
				if ($max_w == $max_h)
					$src_w = $src_h;
				if ($max_w == $max_h)
				{
					$src_x = ($src_w - $src_h) / 2;
					$src_h = $src_w;
				}
				else
				{
					$w = $src_w;
					$src_w = $src_h * ($max_w / $max_h);
					$src_x = ($w - $src_w) / 2;
				}
			}
			elseif ($src_w < $src_h)
			{
				$src_x = 0;
				if ($max_w == $max_h)
				{
					$src_y = ($src_h - $src_w) / 2;
					$src_h = $src_w;
				}
				else
				{
					$h = $src_h;
					$src_h = $src_w * ($max_h / $max_w);
					$src_y = ($h - $src_h) / 2;
				}
			}
		}
		
		// generates resampled image
		$dest_img = imagecreatetruecolor($dest_w, $dest_h);
		
		if (function_exists('imagecopyresampled'))
		{
			if (!imagecopyresampled($dest_img, $this->image, 0, 0, $src_x, $src_y, $dest_w, $dest_h, $src_w, $src_h))
				return false;
		}
		else
		{
			if (!imagecopyresized($dest_img, $this->image, 0, 0, $src_x, $src_y, $dest_w, $dest_h, $src_w, $src_h))
				return false;
		}
		
		// saves resampled image to jpeg (with quality 95)
		if (!imagejpeg($dest_img, $dest_file, 95))
			return false;
		
		// destroys temp images
		if (!imagedestroy($dest_img))
			return false;
		
		return true;
	}
}

?>
